package io.terminus.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.manager.service.SysUserService;
import io.terminus.spzx.model.dto.system.AssignRoleDto;
import io.terminus.spzx.model.dto.system.SysUserDto;
import io.terminus.spzx.model.entity.system.SysUser;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 系统用户Controller
 * @Date 2024-05-23
 */
@RestController
@Tag(name = "系统用户接口")
@RequestMapping(value = "/admin/system/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/findByPage/{current}/{limit}")
    public Result<PageInfo<SysUser>> findByPage(@RequestBody SysUserDto sysUserDto, @PathVariable("current") Integer current, @PathVariable("limit") Integer limit) {
        PageInfo<SysUser> userPageInfo = sysUserService.findByPage(sysUserDto, current, limit);

        return Result.build(userPageInfo, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/saveSysUser")
    public Result<Void> saveSysUser(@RequestBody SysUser sysUser) {
        sysUserService.saveSysUser(sysUser);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/updateSysUser")
    public Result<Void> updateSysUser(@RequestBody SysUser sysUser) {
        sysUserService.updateSysUser(sysUser);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/deleteById/{userId}")
    public Result<Void> deleteById(@PathVariable("userId") Long userId) {
        sysUserService.deleteById(userId);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 给用户分配角色授权
     *
     * @param assignRoleDto 用户角色授权关系
     * @return 返回结果<空>
     */
    @PostMapping("/assignRoleAuth")
    public Result<Void> assignRoleAuth(@RequestBody AssignRoleDto assignRoleDto) {
        sysUserService.assignRoleAuth(assignRoleDto);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
